package com.jp.study.Interceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
/**
 * 
* @author dinghao
* @ClassName: LogMethodInterceptor 
* @Description: 计算方法执行时间
* @date 2018年3月21日 上午10:37:11 
*
 */
@Component
public class LogMethodInterceptor implements MethodInterceptor {
	
    private Logger logger = LoggerFactory.getLogger(LogMethodInterceptor.class);
    private List<String> exclude ;
    public LogMethodInterceptor(String[] exclude) {
        this.exclude = Arrays.asList(exclude);
    }
    
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
    	Method method = invocation.getMethod();
        String methodName = method.getName();
        String declaredClassName=method.getDeclaringClass().getName();
        if(exclude.contains(methodName)) {
            return invocation.proceed();
        }
        long start = System.currentTimeMillis();
        Object result = invocation.proceed();
        long end = System.currentTimeMillis();
        logger.info("类({}),方法({}), 耗时({} ms) ", declaredClassName , methodName, (end - start));
        return result;
    }
}
